package com.tyc.threadlocal;

import java.util.concurrent.TimeUnit;

/**
 * 借助threadlocal实现线程同步
 */
public class Profiler {
    private static final ThreadLocal<Long> start = ThreadLocal.withInitial(() -> System.currentTimeMillis());


    public static final void begin() {
        start.set(System.currentTimeMillis());
    }

    public static final Long end(){
        return System.currentTimeMillis()-start.get();
    }

    public static void main(String[] args) throws InterruptedException {
        Profiler.begin();
        TimeUnit.SECONDS.sleep(1);
        System.out.println(String.format("user time:%ss", Profiler.end()));
    }
}

